package com.thzy.bbs.setting;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.webkit.JavascriptInterface;

import com.alibaba.fastjson.JSON;
import com.thzy.bbs.R;
import com.thzy.bbs.activity.LoginActivity;
import com.thzy.bbs.activity.MainActivity;
import com.thzy.bbs.common.ActivityConfig;
import com.thzy.bbs.common.CommonUtils;
import com.thzy.bbs.common.Constants;
import com.thzy.bbs.common.StringUtils;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;


/**
 * 前后端交互
 *
 * @author Lirui
 */
public class SetFunction {
    private MainActivity mainActivity;
    private SetMainSkip setMainSkip;

    public SetFunction(MainActivity mainActivity, SetMainSkip setMainSkip) {
        super();
        this.mainActivity = mainActivity;
        this.setMainSkip = setMainSkip;
    }


    /**
     * 打开相册
     */
    @JavascriptInterface
    public void openPictures(int nu) {
        setMainSkip.openPictures(nu, Constants.Code.PHOTO_OFTEN_CODE, null);
    }


    /**
     * 打开相册
     */
    @JavascriptInterface
    public void openPictures(int nu, String userId) {
        if (!StringUtils.isBlank(userId)) {
            setMainSkip.openPictures(nu, Constants.Code.PHOTO_HAND_CODE, userId);
        } else {
            openPictures(nu);
        }
    }

    /**
     * 打开相册
     */
    @JavascriptInterface
    public void meipaiOpenPictures(int nu, String isBack) {
        MainActivity.isMeiPaiBack = isBack;
        setMainSkip.openPictures(nu, Constants.Code.PHOTO_OFTEN_CODE, isBack);
    }

    /**
     * 清除缓存（清除缓存的实质是删除缓存文件夹）
     */
    @JavascriptInterface
    public boolean cleanCache() {
        boolean result = setMainSkip.clearWebViewCache();
        return result;
    }

    /**
     * google play
     */
    @JavascriptInterface
    public void toGooglePlay() {
        setMainSkip.goToGooglePlay();
    }

    /**
     * 浏览器跳转
     *
     * @param url 地址
     */
    @JavascriptInterface
    public void toChrome(String url) {
        // 弹出一个选择浏览器的框，选择浏览器再进入
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri contentUrl = Uri.parse(url);
        intent.setData(contentUrl);
        mainActivity.startActivity(Intent.createChooser(intent,
                mainActivity.getString(R.string.toPay)));
    }

    /**
     * 获取Cookies
     */
    @JavascriptInterface
    public String getSessionId() {
        return CommonUtils.session;
    }

    /**
     * 跳转登录
     */
    @JavascriptInterface
    public void goLogin() {
        Intent intent = new Intent(mainActivity, LoginActivity.class);
        mainActivity.startActivity(intent);
        mainActivity.finish();
    }

    /**
     * 跳转登录(账号被挤下线)
     */
    @JavascriptInterface
    public void offline() {
        Intent intent = new Intent(mainActivity, LoginActivity.class);
        intent.putExtra(ActivityConfig.parameterName.OFFLINE, true);
        mainActivity.startActivity(intent);
        mainActivity.finish();
    }

    /**
     * 发送设备号
     */
    @JavascriptInterface
    public String getDeviceNumber() {
        return LoginActivity.TOKEN == null ? Constants.EquipmentInfo.DEFAULT_TOKEN : LoginActivity.TOKEN;
    }

    /**
     * 消息显示
     *
     * @param messages 消息(单条)
     */
    @JavascriptInterface
    public void showMessages(String messages) {
        CommonUtils.showMessage(messages, mainActivity.getWebView(), 2000, Color.parseColor("#acd598"), Color.parseColor("#009b72"));
    }

}
